/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.daos;

import database.orm.ORMException;
import database.databasemanager.exceptions.DataBaseException;

import beans.ProviderProductBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Fam. Tapia
 */
public class ProviderProductDAO extends AbstractDAO<ProviderProductBean> {

    @Override
    public void insert(ProviderProductBean providerProd) throws DAOException {
        try {
                dataBaseManager.openConnection();
                String sqlStatement = "INSERT INTO PROVIDER_PRODUCT(providerId, productId )values('" + providerProd.getProviderId() + "','" + providerProd.getProductId() + "');";
                dataBaseManager.executeUpdate(sqlStatement);
            }
        catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    public void delete(String condition) throws DAOException {
        try {
                dataBaseManager.openConnection();
                String sqlStatement = "DELETE FROM provider_product " + condition;
                dataBaseManager.executeUpdate(sqlStatement);
          }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    public void update(ProviderProductBean providerProduct, String  condition) throws DAOException {
        try {
                dataBaseManager.openConnection();
                String sqlStatement = "UPDATE provider_product SET providerId='" + providerProduct.getProviderId() + "',productId='" + providerProduct.getProductId() + "' " + condition;
                dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }    
    }

    @Override
    protected ArrayList<ProviderProductBean> searchAll(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
             String sqlStatement = "SELECT * FROM PROVIDER_PRODUCT ";
             ArrayList<ProviderProductBean> providerProductBeanList = null;
             providerProductBeanList = createProviderBeanList(dataBaseManager.executeQuery(sqlStatement));
             return providerProductBeanList;
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }

      
        }


    @Override
    protected ProviderProductBean search(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM PROVIDER_PRODUCT ";
            ProviderProductBean providerProductBean= null;
            providerProductBean = createProviderBean(dataBaseManager.executeQuery(sqlStatement));
            return providerProductBean;
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }     
    }




 private ArrayList<ProviderProductBean> createProviderBeanList(ResultSet queryResult) throws DAOException{
        try {
            ArrayList<ProviderProductBean> providerProductBeansList = new ArrayList();
            while (queryResult.next()) {
                providerProductBeansList.add((ProviderProductBean) beansGenerator.generateBean(queryResult, ProviderProductBean.class));
            }
            return providerProductBeansList;
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception){
            throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }

    private ProviderProductBean createProviderBean(ResultSet queryResult) throws DAOException{
        try {
            moveQueryResultToBegin(queryResult);
            return (ProviderProductBean) beansGenerator.generateBean(queryResult, ProviderProductBean.class);
        } catch (ORMException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        }
    }

    public ProviderProductBean searchProviderProduct(int productId, int providerId) throws DAOException{
        return search("WHERE productId='"+productId+"' and providerId='"+providerId+"';");
    }


     public void moveQueryResultToBegin(ResultSet queryResult) throws SQLException{
        queryResult.first();
    }


    }

